package com.example.demo.config;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-31
 * Time: 15:29
 */

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义用户登录的拦截器
 */
@Component
public class LoginIntercept implements HandlerInterceptor {
    /**
     * 返回true拦截通过 返回false拦截不通过
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        ///1.得到HttpSession对象
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("userinfo") != null){
            //表示已经登录
            return true;
        }
        return false;
    }
}
